package qeb.handheldmedical.adapter;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import qeb.handheldmedical.R;
import qeb.handheldmedical.adapter.LisAdapter.ViewHolder;
import qeb.handheldmedical.helper.WSHelper;
import android.R.bool;
import android.content.Context;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * @author 作者(dugaoyi) E-mail:dugaoyi@163.com
 * @version 创建时间：2016年8月30日 上午10:40:26
 * @declaration 在线学习设备器
 */
public class LineExamAdapter extends BaseAdapter {

	private static final String TAG = "learningJsonArray";

	private int mCount;
	private Context mContext;
	private JSONArray mLearningJsonArray = null;
	private Boolean IsExam = false;// 是不是考试
	private Boolean isSign = false;// 签到/签退
	private String userGh = "";// 用户工号
	private String examType = "";

	public LineExamAdapter(int count, Context context,
			JSONArray learningJsonArray) {
		this.mCount = count;
		this.mContext = context;
		this.mLearningJsonArray = learningJsonArray;
	}

	public LineExamAdapter(int count, Context context,
			JSONArray learningJsonArray, Boolean IsExam) {
		this.mCount = count;
		this.mContext = context;
		this.mLearningJsonArray = learningJsonArray;
		this.IsExam = IsExam;
	}

	public LineExamAdapter(int count, Context context,
			JSONArray learningJsonArray, Boolean isSign, String userGh) {
		this.mCount = count;
		this.mContext = context;
		this.mLearningJsonArray = learningJsonArray;
		this.isSign = isSign;
		this.userGh = userGh;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mCount;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewholder = null;
		convertView = LayoutInflater.from(mContext).inflate(
				R.layout.adapter_exam_line, null);
		viewholder = new ViewHolder();
		viewholder.tv_learningName = (TextView) convertView
				.findViewById(R.id.tv_learningName);
		convertView.setTag(viewholder);
		String examName = "";
		try {
			JSONObject xt = mLearningJsonArray.getJSONObject(position);
			if (xt != null) {
				// 开通试卷
				if (IsExam) {
					String examState = xt.getString("EXAMSTATE");

					if (examState.equals("0")) {
						examName = "<font color=\"#aabb00\">【已开放】</font>";
					} else {
						examName = "<font color=\"#FF4500\">【未开放】</font>";
					}
					examName += xt.getString("TITLE");

					viewholder.tv_learningName.setText(Html.fromHtml(examName));
				}
				// 签到/签退
				else if (isSign) {
					String studyid = xt.getString("STUDYID");
					String examid = xt.getString("EXAMID");

					Map<String, String> map = new HashMap<String, String>();
					map.put("studyid", studyid);
					map.put("examid", examid);
					map.put("userid", userGh);
					JSONArray signArray = WSHelper.getWebserviceFun(
							"SelectStudySign", map);
					if (signArray != null && signArray.length() > 0) {
						JSONObject signObject = signArray.getJSONObject(0);
						// 签退
						if (!signObject.getString("SIGNTIME").equals("")) {
							examName = "<font color=\"#FF4500\">【已签退】</font>";

						}
						// 签到
						else if (!signObject.getString("SIGNOUTTIME")
								.equals("")) {
							examName = "<font color=\"#aabb00\">【已签到】</font>";
						}

						examName += xt.getString("TITLE");
						viewholder.tv_learningName.setText(Html
								.fromHtml(examName));
					}
				}
				// 默认
				else {
					String examType = xt.getString("EXAMTYPE");
					String expertState = xt.getString("EXPERTSTATE");
					examName = xt.getString("TITLE");
					// 专家面授学习
					if (examType.equals("4")) {

						if (expertState.equals("0")) {
							examName = "<font color=\"#aabb00\">【已学习】</font>";
						} else {
							examName = "<font color=\"#FF4500\">【未学习】</font>";
						}

						examName += xt.getString("TITLE");
						viewholder.tv_learningName.setText(Html
								.fromHtml(examName));
					} else {
						viewholder.tv_learningName.setText(examName);
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			Log.e(TAG, e.toString());
		}
		return convertView;
	}

	static class ViewHolder {
		// ImageView imgPatient;
		TextView tv_learningName;
	}
}
